Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(monitor): introduce MonitorStatus #780

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from

Conversation

FalcoSuessgott
Copy link

@FalcoSuessgott FalcoSuessgott commented Sep 19, 2024

What does this do?

In regard of #775, This PR introduces a new interface MonitorStatus that encapsulates Monitor but adds RecordJobTimingStatus(startTime, endTime time.Time, id uuid.UUID, name string, tags []string, status JobStatus, err error), to allow recording a Job with its name, tags, start & end time, as well as the job status and error. I choose to add a new interface instead of changing Monitor to prevent introducing a breaking change.

Which issue(s) does this PR fix/relate to?

Fixes #775

List any changes that modify/break current functionality

none

Have you included tests for your changes?

Did you document any new/modified functionality?

  • Updated example_test.go (ExampleWithMonitor() is commented out :D)
  • Updated README.md

Notes

I believe having the JobStatus and error in RecordJobTiming, definitely make sense, in case you're fine with it we can also just extend Monitor even if it means having a breaking change ...

@FalcoSuessgott
Copy link
Author

FalcoSuessgott commented Sep 19, 2024

Tested it on my end with replace github.com/go-co-op/gocron/v2 v2.12.0 => github.com/FalcoSuessgott/gocron/v2 v2.12.2 in my go.mod.

// New returns a new scheduler.
func New(monitor gocron.MonitorStatus) Scheduler {
	s, err := gocron.NewScheduler(
		gocron.WithMonitorStatus(monitor),
		gocron.WithLogger(slog.Default()),
		gocron.WithGlobalJobOptions(
			gocron.WithSingletonMode(gocron.LimitModeReschedule),
		),
	)
	if err != nil {
		log.Fatalf("could not start scheduler: %v", err)
	}

	return Scheduler{s}
}

MonitorStatus implementation:

func (c *Collector) RecordJobTimingWithStatus(startTime, endTime time.Time, uid uuid.UUID, name string, _ []string, status gocron.JobStatus, err error) {
	log.Printf("[%s (%s)] %s - %s => %v\n", name, uid, startTime, endTime, status)
}

func (c *Collector) RecordJobTiming(startTime, endTime time.Time, _ uuid.UUID, name string, _ []string) {}

func (c *Collector) IncrementJob(id uuid.UUID, name string, tags []string, status gocron.JobStatus) {}

And the output is exactly what I need:

....
2024/09/20 09:45:14 [vmware_vm (23dbda12-79ec-4f5d-989a-c240dd636639)] 2024-09-20 09:45:14.468317 +1000 AEST m=+13.162815626 - 2024-09-20 09:45:14.769485 +1000 AEST m=+13.463985126 => success
....

@FalcoSuessgott FalcoSuessgott changed the title feat(monitor): introduce MonitorWithStatus feat(monitor): introduce MonitorStatus Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] - Add JobStatus to RecordJobTiming in Monitor Interface
1 participant